#pragma once

#include "Framework/ApplicationSubsystem.h"
#include "Framework/Tickable.h"
#include "Path/Path.h"
#include "Audio/AudioPlayer.h"
#include "AudioSubsystem.m.h"

class RAudio;


using PlayerHandle=uint64_t;

RCLASS(ExtraHeader=["Audio/Audio.h", "Audio/AudioPlayer.h"])
class WHENGINE_API RAudioSubsystem : public RApplicationSubsystem , public ITickable
{
    ROBJECT

public:
    virtual void Initialize() override;
    virtual void Deinitialize() override;

    RFUNCTION()
    static RAudioSubsystemPtr GetInstance();

    //check if the file is a wav file
    //use the first 12 bytes to check
    RFUNCTION()
    bool IsWavFile(const TVector<uint8_t>& InFileDataHead);

    RFUNCTION()
    bool IsWavFile(const SPath& InFilePath);

    RFUNCTION()
    TSharedObjectPtr<RAudio> LoadWavFile(const TVector<uint8_t> &InFileData);

    RFUNCTION()
    TSharedObjectPtr<RAudio> LoadWavFile(const SPath &InFilePath);

    //directly play audio once,ignore the position of the audio
    RFUNCTION()
    void PlaySound(TSharedObjectPtr<RAudio> InAudio);

    RFUNCTION()
    TSharedObjectPtr<RAudioPlayer>  CreateAudioPlayer(TSharedObjectPtr<RAudio> InAudio);

protected:
    virtual void Tick(float DeltaSeconds) override;

    //audio players that are playing
    TVector<TSharedObjectPtr<RAudioPlayer> > AudioPlayers;
};